package fuzzy;

public class FuzzyMemFunction {

  enum Type {
    GAUSSIAN, SIGMOID, REVERSED_SIGMOID 
  }
  
  String name;
  Type type;
  double sigma;
  double center;
  
  private FuzzyMemFunction(Type type, String name, double sigma, double center) {
    this.type = type;
    this.name = name;
    this.sigma = sigma;
    this.center = center;
  }
  
  public static FuzzyMemFunction newGaussian(final String name, final double sigma, final double center) {
    return new FuzzyMemFunction(Type.GAUSSIAN, name, sigma, center);
  }
  
  public static FuzzyMemFunction newSigmoid(final String name, final double sigma, final double center) {
    return new FuzzyMemFunction(Type.SIGMOID, name, sigma, center);
  }
  
  public static FuzzyMemFunction newReversedSigmoid(final String name, final double sigma, final double center) {
    return new FuzzyMemFunction(Type.REVERSED_SIGMOID, name, sigma, center);
  }
  
  public double getMemValue(double input) {
    
    switch (type) {
      case GAUSSIAN:
        return Math.exp(-0.5 * Math.pow( (input - center) / sigma, 2));
      case SIGMOID:
        if (input >= center) {
          return 1;
        }
        else {
          return Math.exp(-0.5 * Math.pow( (input - center) / sigma, 2));
        }        
      case REVERSED_SIGMOID:
        if (input <= center) {
          return 1;
        }
        else {
          return Math.exp(-0.5 * Math.pow( (input - center) / sigma, 2));
        }    
    }    
    return input;
  }
  
}
